package com.bishe.util;

import cn.hutool.core.io.FileUtil;
import com.bishe.entity.FileInfo;
import lombok.SneakyThrows;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

public class FileUtils {

    /**
     * 获取存储文件路径
     * @return
     */
    public static String getDirPath(){
        File file = new File(".files");
        //返回绝对路径
        return file.getAbsolutePath();
    }

    /**
     * 下载文件
     * @param response
     * @param file
     */
    public static  void download(HttpServletResponse response, File file){
        try {
            //获取文件流
            FileInputStream fileInputStream = new FileInputStream(file);
            //文件大小
            int available = fileInputStream.available();
            //初始化缓存
            byte[] bytes = new byte[available];
            //写入缓存中
            fileInputStream.read(bytes);
            //获取文件名
            String name = file.getName();
            //设置传输文件类型 和文件名
            response.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode(name,"utf-8"));
            //获取response 输入流
            ServletOutputStream outputStream = response.getOutputStream();
            //写入数据
            outputStream.write(bytes);
            //关闭流
            outputStream.close();
            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 上传文件
     * @param file
     * @return
     */
    @SneakyThrows
    public static String uploadFile(MultipartFile file,String id){
        //获取文件名
        String name = file.getOriginalFilename();
        //将所有下划线替换成- 以防影响真实名切割
        name = name.replace("_", "-");
        //获取文件大小
        long size = file.getSize();
        //转换成M(兆)
        double m = (double)size/1024/1024;
        //文件后缀
        String suffix = FileUtil.getSuffix(name);
        //是图片
        if ("png".equalsIgnoreCase(suffix)||"jpg".equalsIgnoreCase(suffix)){
            //超过10M 不能上传
            if (m > 10){
                return "";
            }
        }
        //超过200M不能上传
        if (m > 200){
            return "";
        }
        //设置一个不重复的文件名
        String fileName =  id+"." + FileUtil.getSuffix(name);
        //获取输出流
        InputStream inputStream = file.getInputStream();
        int available = inputStream.available();
        //缓存
        byte[] bytes = new byte[available];
        //读
        inputStream.read(bytes);
        //获取存放文件的目录路径
        String filePath = getDirPath();
        //生成一个不重名的地址
        String path = filePath +File.separator+fileName;
        System.out.println("path = " + path);
        //获取输入流
        FileOutputStream fileOutputStream= new FileOutputStream(new File(path));
        //写
        fileOutputStream.write(bytes);
        //刷新
        fileOutputStream.flush();
        fileOutputStream.close();
        inputStream.close();
        return fileName;
    }

    /**
     * 获取文件
     * @return
     */
    public static File getFile(FileInfo fileInfo){
        String fileName = fileInfo.getName();
        String dirPath = getDirPath();
        //文件路径
        String filePath = dirPath +File.separator + fileName;
        File file = new File(filePath);
        return file;
    }
}
